package cn.tx.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class MyDateConverter extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		Date date = null;
		if(values != null && values.length > 0){
			if(toClass == Date.class || toClass == Timestamp.class){
				try {
					if(values[0] != null&& !"".equals(values[0])){
						date = new SimpleDateFormat("yyyy-M-d").parse(values[0]);
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}
		return date;
	}

	@Override
	public String convertToString(Map context, Object o) {
		String dateStr = null;
		if(o != null && (o.getClass() == Date.class || o.getClass() == Timestamp.class)){
			dateStr = new SimpleDateFormat("yyyy-M-d").format((Date)o);
		}
			
		return dateStr;
	}


}
